// JavaScript Document
function focuson(obj){
	obj.value = "";
}
function focusout(obj,info){
	if(obj.value == "")
		obj.value = info;
}
function exituser(){
	window.location="main/exit.do";
}

function changeBackColor(obj,col){
	obj.style.cursor = "pointer";
	obj.style.backgroundColor = col;
}
function cbp(obj,url){
	obj.style.cursor = "pointer";
	if(obj.tagName == 'IMG'){
		obj.src = url;		
	}
	else{
		obj.children[0].src = url;	
	}
}



/******心情动态、博客、花园、帖子信息展示******/
var activeInfo = function(){
	var tag = function(){
		//需要动态添加数据的对象
		var obj ;
		//数据数组
		var arr ;
		var init = function(){
			obj = document.getElementById("mcinfo");
		}
		this.insertBefore = function(d){
			obj = document.getElementById("mcinfo");
			//this.init();
			arr = d;
			for(var i = 0 ; i<arr.length ; i++){
				var ps = document.getElementById("mcnav");
				var frame = getFrame();	
				var image = addActive(frame,arr[i]);
				BindOnclickEvent(image);
				obj.insertBefore(frame,ps.nextSibling);
			}
		}
		this.addNode = function(d){
			//var self = this;
			init();
			var frag = document.createDocumentFragment();
			//传入的a不为数组
			arr = d;
			if(arr == null||!arr instanceof Array){
				return;
			}
			//此处接受到arr的json格式必须规范，否则展示结果会出现意想不到的不规范
			//{"image":"image","name":"name","content":"content","time":"time","reply":{}}
			//var arr = this.getArr();
			if(arr.length>0){
				for(var i = 0 ; i<arr.length ; i++){
					var frame = getFrame();	
					addActive(frame,arr[i]);
					obj.appendChild(frame);
				}
			}
			else{
				var frame = getFrame();
				var h2 = document.createElement("h2");
				h2.innerHTML = "无任何动态信息";
				h2.style.cssText = "color:#ccc;margin-top:20px;";
				frame.appendChild(h2);
				frag.appendChild(frame);
			}
			obj.appendChild(frag);
		}
		//添加一条新的动态框
		var getFrame = function(){
			var frame = document.createElement("section");
			var frameCss ="overflow:hidden;display:inline-block;position:relative;padding:10px 0px 15px 0px;border-bottom:1px solid #dfd3a4;height:auto;min-height:50px;width:100%;";
			addCss(frame,frameCss);
			frame.id = "ajaxInfo";
			return frame;
		}
		//添加一条新的动态
		var addActive = function(node,content){
			//JS创建标签对象
			//头像
			addClear(node);
			var image = document.createElement("img");
			var imageCss = "margin-top:5px;width:50px;height:50px;float:left;";
			image.src = content.image;
			image.module = "user";
			image.targetId = content.userId;
			addCss(image,imageCss);
			node.appendChild(image);
			//node.articleId = content.articleId;
			//动态内容
			var frame = getFrame();
			var frameCss = "border:none;float:right;width:85%;height:auto;padding:0px 20px 0px 0px;";
			addCss(frame,frameCss);
			
			var name = document.createElement("h2");
			var nameCss = "font:14px normal;position:relative;float:left;color:#7c5c45;display:block;";
			var active = null;;
			var activeCss = null;
			if(content.articleId){
				//赋值
				name.innerHTML = content.name;
				active = document.createElement("p");
				activeCss = "margin:5px 0px;float:left;font-size:12px;color:#4E443B;display:block;width:100%;word-wrap: break-word;height:auto;text-align:left;";
				active.innerHTML = content.content;
				active.targetId = content.articleId;
				frame.articleId = content.articleId;
			}
			else if(content.messageId){
				//赋值
				name.innerHTML = content.name;
				active = document.createElement("p");
				activeCss = "margin:5px 0px;float:left;font-size:14px;color: #4E443B;display:block;width:100%;word-wrap: break-word;height:auto;text-align:left;";
				active.innerHTML = content.content;
				active.messageId = content.messageId;
				frame.messageId = content.messageId;
			}
			else if(content.pictureId){
				//赋值
				name.innerHTML = content.name+" 上传照片";
				active = document.createElement("img");
				activeCss = "margin:5px 0px;float:left;display:block;min-height:50px;max-height:400px;height:auto;min-height:50px;max-width:400px;width:auto;";
				//node.image = value;
				reloadPicture(active,content.content);
				//active.image = content.content;
				active.module = "picture";
				active.targetId = content.pictureId;
				frame.pictureId = content.pictureId;
			}
			
			//设置CSS样式
			
			
			addCss(name,nameCss);
			addCss(active,activeCss);
			//把标签对象添加到node中
			
			frame.appendChild(name);
			addClear(frame);	
			frame.appendChild(active);
			addTime(frame,content.time,true);
			if(content.reply != null){
				var reply = content.reply;
				for(var i = 0;i<reply.length;i++){
					addReply(frame,reply[i]);
				}
			}
			//添加回复框
			addArea(frame);
			
			node.appendChild(frame);
			return image;
		}
		//添加该动态的好友评论
		var addReply = function(node,content){
			addClear(node);
			//头像
			var image = document.createElement("img");
			var imageCss = "margin-top:5px;width:30px;height:30px;float:left;font-size:14px;color: #4E443B;";
			image.src = content.image;
			image.module = "user";
			image.targetId = content.userId;
			addCss(image,imageCss);
			node.appendChild(image);
			var frame = replyInfo(content);
			node.appendChild(frame);
		}
		//添加该动态的好友评论时间
		var addTime = function(node,content,bo){
			addClear(node);
			var ul = document.createElement("ul");
			var ulCss = "width:100%;height:auto;display:block;";
			addCss(ul,ulCss);
			var li_1 = document.createElement("li");
			var liCss = "color:#9c9c9c;font-size:14px;margin:0px 10px 0px 0px;";
			li_1.innerHTML = content;
			addCss(li_1,liCss);
			ul.appendChild(li_1);
			if(!bo){
				node.appendChild(ul);
				return;
			}
			//if(content.length>1){
			//	for(var i =1;i<content.length;i++){
			var li_2 = document.createElement("li");	
			li_2.innerHTML = "<a style='color:#976945;font-size:12px;' href='#'>赞</a>";
			//li_2.innerHTML = "<a style='color:#976945;font-size:12px;' href='#'>"+content[i]+"</a>";					
			//	}
			//}
			
			var li_3 = document.createElement("li");
			var li_4 = document.createElement("li");
			var li_5 = document.createElement("li");
			
			li_3.innerHTML = "<a style='color:#976945;font-size:12px;' href='#'>评论(0)</a>";
			li_4.innerHTML = "<a style='color:#976945;font-size:12px;' href='#'>转发</a>";
			li_5.innerHTML = "<a style='color:#976945;font-size:12px;' href='#'>浏览(0)</a>";
			
			addCss(li_2,liCss);
			addCss(li_3,liCss);
			addCss(li_4,liCss);
			addCss(li_5,liCss);		
			
			ul.appendChild(li_2);
			ul.appendChild(li_3);
			ul.appendChild(li_4);
			ul.appendChild(li_5);
			node.appendChild(ul);
		}
		//isDotted=true为虚线，否则为实线
		var addLine = function(node,isDotte){
			var line = document.createElement("hr");
		}
		var reply = function(node,flow,data){
			//头像
			var image = document.createElement("img");
			var imageCss = "clear:both;margin-top:5px;width:30px;height:30px;float:left;";
			image.src = data.image;
			addCss(image,imageCss);
			node.insertBefore(image,flow.previousSibling);
			//node.appendChild(image);
			var frame = replyInfo(data);
			node.insertBefore(frame,flow.previousSibling);
			//node.appendChild(frame);
			//node.appendChild(frame);
			scrollsTo();
		}
		var replyInfo = function(content){
			
			//动态内容
			var frame = getFrame();
			var frameCss = "min-height:auto;border:none;font-size:14px;color: #4E443B;float:right;width:91%;height:auto;padding:0px;";
			addCss(frame,frameCss);
			
			//var name = document.createElement("h2");
			var art = document.createElement("p");
			//赋值
			//name.innerHTML = content.name;
			art.innerHTML = "<h4 style='font-weight:normal;color:#7c5c45;float:left;'>"+content.name+"</h4>" + " :" + content.content;
			//设置CSS样式
			//var nameCss = "font-weight:normal;position:relative;float:left;color:#7c5c45;display:block;";
			var artCss = "word-wrap: break-word;margin:5px 0px 5px 0px;float:left;font-size:14px;color: #4E443B;display:block;width:100%;height:auto;text-align:left;";
			//this.addCss(name,nameCss);
			addCss(art,artCss);
			//把标签对象添加到node中
			
			//frame.appendChild(name);
			addClear(frame);	
			frame.appendChild(art);
			addTime(frame,content.time,false);
			addClear(frame);
			if(content.reply != null){
				var reply = content.reply;
				for(var i = 0;i<reply.length;i++){
					addReply(frame,reply[i]);
				}
			}
			return frame;
		}
		//添加回复框
		var addArea = function(node){
			addClear(node);
			var area = document.createElement("textarea");
			var buttom = document.createElement("a");
			buttom.href = "#";
			buttom.style.cssText = "float:right;margin:3px -8px 0px 0px;";
			image = document.createElement("img");
			image.src = '/networkforum/images/unp.jpg';
			buttom.appendChild(image);
			area.style.cssText = "overflow:hidden;padding:3px;color:#a8a8a8;font-size:12px;margin-top:5px;border:1px solid #ccc;width:100%;height:18px;";
			image.onclick = function(ev){
				scrollInit();
				var url;
				var n = node;
				if(node.articleId){
					var arr = [node.articleId,area.value];
					url = "/networkforum/user/article_reply.do?replyInfo="+encodeURIComponent(arr);
				}
				else if(node.messageId){
					url = "/networkforum/messages/message_reply.do?messageId="+node.messageId+"&replyContent="+encodeURIComponent(area.value);
				}
				else{
					url = "/networkforum/picture/picture_reply.do?pictureId="+node.pictureId+"&content="+encodeURIComponent(area.value);
				}
				//登录页面后通过myajax获取进一步的信息
				var myajax = getByAjax();
				myajax.init(null,"get",true);
				myajax.get(url,function(d){
					//var data = eval("("+d+")");
					var data = toJson(d);
					
					reply(node,area,data);
				});
				myajax = null;
				node.removeChild(buttom);
				area.style.height = "18px";
				area.value = "我也说一句";	
			}
			area.onclick = function(){
				area.style.height = "50px";	
				if(area.value == "我也说一句")
					area.value = "";	
				node.appendChild(buttom);
			}
			area.onblur = function(){
				if(area.value == ""){
					area.style.height = "18px";
					area.value = "我也说一句";	
					node.removeChild(buttom);
				}
			}
			
			area.value = "我也说一句";

			node.appendChild(area);
		}
		var addClear = function(node){
			var div = document.createElement("div");
			div.className = "clear";
			node.appendChild(div);		
		}
		var addCss = function(node,cssText){
			var ct = node.style.cssText;
			node.style.cssText = ct + cssText;
		}
	}
	return new tag();
}

//修改window默认行为
/*window.alert = function(d){
	var a = alert(d);
}*/
function scrollInit(){
	sx = window.scrollX;
	sy = window.scrollY;
}
var sx = 0;
var sy = 0;
function scrollsTo(){
	setTimeout(function(){
		window.scrollTo(sx,sy);
	},10)
}
/********************创建隔离层*********************/
var ALPHA = function(){
	var alpha = function(){
		this.container = null;
		this.div = null;
	}
	alpha.prototype = {
		addTo:function(container){
			var self = this;
			self.container = container;
			var div = document.createElement("div");
			div.className = "alpha8 back";
			div.id = "back";
			with(div.style){
				width = container.offsetWidth;
				height = container.offsetHeight;
				position = "fixed";	
			}
			self.div = div;
			container.appendChild(div);
		},
		remove:function(){
			var self = this;
			self.container.removeChild(self.div);
		}
	}
	return new alpha();
}
/****************跑马灯效果*******************/
/*********************跑马灯效果****************/
var SCROL = function(){
	var scrol = function(){
		this.target = null;
		this.speed = 100;
		this.face = "left";
		this.distance = 2;
		this.timer = null;
		
		this.width = null;
		this.left = 0;
	}
	scrol.prototype = {
		init:function(target,distance,speed,face){
			var self = this;
			self.target = target;
			self.distance = distance;
			self.speed = speed;
			self.face = face;
			
			self.width = target.children[0].offsetWidth;
		},
		scrol:function(){
			var self = this;
			self.timer = setInterval(function(){
				self.move();	
			},self.speed);
		},
		move:function(){
			var self = this;
			if(self.target == null){
				return ;	
			}
			self.left = self.left - self.distance;
			//self.left = self.left - 2;
			self.target.style.left = self.left + "px";	
			
			self.adjust();
		},
		mvoeTo:function(distance){
			var self = this;
			if(self.target == null){
				alert("多建几个相册试试！");
				return;
			}
			clearInterval(self.timer);
			var dis = 0;
			
			self.timer = setInterval(function(){
				self.target.style.left = self.target.offsetLeft + distance/10 + "px";
				if(++dis == 10){
					clearInterval(self.timer);
					self.left = self.left + distance;
					self.scrol();	
				}
				self.adjust();
			},self.speed);
		},
		adjust:function(){
			var self = this;
			if(self.left + self.width <0){
				var first = self.target.children[0];
				self.target.removeChild(first);
				self.target.appendChild(first);
				self.left = self.left + self.width;	
			}
			else if(self.left>0){
				var last = self.target.children[self.target.children.length-1];
				self.target.removeChild(last);
				self.target.insertBefore(last,self.target.children[0]);
				self.left = self.left - self.width;
			}
		},
		clear:function(){
			var self = this;
			clearInterval(self.timer);
		}
	}
	return new scrol();	
}
function BindOnclickEvent(target){
	target.onclick = function(event){
		var e = event || window.event;
		//alert(e.target.module+"  "+e.target.targetId);
		var target = e.target;
		if(target.module == "user"){
			location = "/networkforum/visit/visit_user.do?targetId="+target.targetId;
		}
		else{
			location = "/networkforum/visit/visit_picture.do?isgroup=false&targetId="+target.targetId;
		}
	}
}

function leaveMessage(url,container){
	var userId = document.getElementById("targetId").value;
	var alpha = new ALPHA();
	alpha.addTo(container);
	var iframe = document.createElement("iframe");
	iframe.id = "messageIframe";
	iframe.src = url;
	if(userId.indexOf('/')>0){
		iframe.userId = userId.substring(0,userId.length-1);
	}
	else{
		iframe.userId = userId;
	}
	iframe.className = "messageIframe";
	//window.document.body.appendChild(iframe);
	container.appendChild(iframe);
}

/*****************绑定导航栏用户名称*********************/
function bindUser(un){
	var tnav= document.getElementById("tnav");
	if(tnav==null){
		return;
	}
	var doc = document.frames ? document.frames["tnav"].document : ifm.contentDocument;
	doc.getElementById("userLi").style.display = "";
	var userName = doc.getElementById("userName");
	userName.innerHTML = un;
}
function reloadPicture(img,url){
	if(url==null){
		return;
	}
	var path = url.substring(0,url.lastIndexOf("."))
	if(url.indexOf(".jpg")>0){
		img.src = path+".png";
	}
	else{
		img.src = path+".jpg";
	}
	img.image = url;
	img.onload = function(event){
		event = event||window.event;
		var target = event.target;
		target.src = target.image;
		target.onload = null;
		target = null;
		if(window.parent&&window.parent.iFrameHeight){
			window.parent.iFrameHeight();
		}
	};
}
function systemStyle(data,module){
	var url = "";
	for(var i =0;i<data.length;i++){
		var d = data[i];
		if(module == d.moduleName){
			url = d.moduleCss;
			break;
		}
	}
	
	var style = document.createElement("link");
	style.type = "text/css";
	style.rel = "stylesheet";
	style.href = url;
	
	var header = document.getElementsByTagName("head")[0];
	header.appendChild(style);
}